﻿var ip = "http://192.168.27.231:81";
var javabatch = 62;
var validator;
//初始化日期控件（需要控制后面日期不能大于前面日期）
function datePluginInit(){
	 $('#graduateTime').datepicker({
		 format : 'yyyy-mm-dd',
		 language : 'zh-CN',
		 autoclose:true,
		 clearBtn : true,
		 todayHighlight : true
	 });
	 $('#ruzhiTime').datepicker({
		 format : 'yyyy-mm-dd',
		 language : 'zh-CN',
		 autoclose:true,
		 clearBtn : true,
		 todayHighlight : true
	 });
}

//初始化校验规则
function validatorInit(){
	/**手机*/
	jQuery.validator.addMethod("phone", function(value,element,param) {
		var phone = /^1[2|3|5|6|7|8|9]\d{9}$/;
		return this.optional(element) || phone.test(value);
	}, "请输入有效的手机号码!");
	
	validator = $("#saveForm").validate({
		rules:{
			name:{
				required:true
			},
			age:{
				required:true,   
				digits:true,				
			    range:[18,35]
			},
			sex:{
				required:true
			},
			phone:{
				required:true,   
				phone:true
			},
			hopeSalary:{
				number:true
			},
			salary:{
				number:true
			},
			education:{
				required:true
			},
			javaBatch:{
				required:true,
				digits:true
			}
		},
		messages:{
			name:{
				required:"必须填写姓名"
			},
			age:{
				required:"必须填写年龄",
				digits:"年龄必须是整数",  
				range:"年龄应在18-35岁之间"
			},
			sex:{
				required:"必须填写性别",
			},
			phone:{
				required:"必须填写手机号"
			},
			hopeSalary:{
				number:"薪水必须是数字"
			},
			salary:{
				number:"薪水必须是数字"
			},
			education:{
				required:"必须填写学历"
			},
			javaBatch:{
				required:"必须填写班级",
				digits:"班级必须是整数"
			}
		},
	});
}

function jumpPage(){
	var url = "log-msg.html?username=abc123";
	url = encodeURI(url);
	window.location.href = encodeURI(url);
	//window.open(url, "", "width=1200,height=1200");
	
	var url = decodeURI(window.location.href);
	var argsIndex = url .split("?username=");
	var arg = argsIndex[1];
	alert(arg);
}

//绑定事件
function bindInit(){
	//为页码大小选择框绑定改变事件
	$("#pageSizeView").bind("change",function(){
		$("#currentPage").val(1);
		$("#pageSize").val($("#pageSizeView").val());
		submitQueryForm();
	});
	
	//为上传按钮绑定click事件
	$("#btn-import-data").bind("click",function(){
		var viewSelf = this;
		var $modal = $("#importModal");
		var $form = $("#form-importData");
		var $btnSelf = $("#btn-import-data");
		//按钮进入loading状态
		$($btnSelf).button('loading');;
		var options  = {   
			type: 'post',   
			url: ip + "/heiMaStudentMsg/importExcel",    
			success:function(result){    
				if(result&&result.success){
					$.alert({
						title : '提示信息!',
						content : result.msg,
						confirm : function() {
							$modal.modal("hide");
							$form.resetForm();  
							search();
						}
					});
				}else{
					$.alert(result.msg);
				}
				//按钮重置
				$($btnSelf).button('reset');;
			},
			error:function(){
				//按钮重置
				$form.resetForm();  
				$($btnSelf).button('reset');
			}          
		  };
		$form.ajaxSubmit(options);  
	})
}

//重置表单
function resetQueryForm(){
	$.each($("#queryForm").find(":text,select,textarea"), function() {
		$(this).val("");
	});
	//重置域
	$("#javaBatch").val(javabatch);
	$("#currentPage").val(1);
	$("#currentPageView").val(1);
	//清空表格
	$("#studentMsgInfo-tbody").html("");
}

function resetSaveForm(){
	$.each($("#saveForm").find(":text,select,textarea,#graduateTime,#ruzhiTime"), function() {
		$(this).val("");
	});
	$("#sex").val("男");
}

//指定页面跳转
function jump(page){
	//上一页
	if(page == 'pre'){
		var nowpage = Number($("#currentPage").val()) - 1;
		if(nowpage < 1){
		   var nowpage = Number($("#currentPage").val());
		}
		$("#currentPage").val(nowpage);
	//下一页
	}else if(page == 'next'){
		var nowpage = Number($("#currentPage").val()) + 1;
		var maxpage = Number($("#pageCount").html());
		if(nowpage > maxpage){
		   var nowpage = Number($("#currentPage").val());
		}
		$("#currentPage").val(nowpage);
	//最后一页
	}else if(page == 'last'){
		$("#currentPage").val($("#pageCount").html());
	}else{
		var nowpage = Number(page)
		if(nowpage < 1){
			$("#currentPage").val(1);
		}else{
			$("#currentPage").val(page);
		}
	}
	submitQueryForm();		
}

//打开添加学生信息模态框
function openAddModal(){
	$("#id").val("");
	resetSaveForm();
	$("#sex").val("男");
	$("#saveModal").modal('show');
}

//打开批量导入学生信息的模态框
function openImportModal(){
	$("#importModal").modal('show');
}

//打开编辑学生信息模态框
function openEditModal(id){
	$.ajax({   
		type : "get",
		dataType : "jsonp",
		jsonp:"callback",    
		jsonpCallback:"success_jsonp",  
		data : {
			"id":id
		},
		url : ip + "/heiMaStudentMsg/queryById",
		success : function(result){
			if(result.success){
				//重置表单
				resetSaveForm();
				$("#id").val(result.data['id']);
				//填充表单
				$.each($("#saveForm").find(":text,select,textarea,#graduateTime,#ruzhiTime"), function() {
					$(this).val(result.data[$(this).attr("name")]);
				});
				//打开modal
				$("#saveModal").modal('show');
			}else{
				$.alert("查询失败！")
			}
		}
	});
}
//删除学生信息
function deleteMsg(id){
	bootbox.dialog({
		title: "确定删除该条记录？",
		message: "tips:删除操作不可逆，请谨慎操作",
		buttons: {
		  danger: {
			label: "确定",
			className: "btn-danger",
			callback: function() {
				$.ajax({
					type : "delete",
					dataType : "jsonp",
					jsonp:"callback",    
					jsonpCallback:"success_jsonp",  
					data : {
						"id":id
					},
					url : ip + "/heiMaStudentMsg/delete",
					success : function(result){
						if(result.success){
							$.alert({
								title : '提示信息!',
								content : '删除成功',
								confirm : function() {
									search();
								}
							});
						}else{
							$.alert("删除失败！")
						}
					}
				});
			}
		  },
		  success: {
			label: "取消",
			className: "btn-success"
		  }
		}
   });
}
//搜索
function search(){
	$("#currentPage").val(1);
	submitQueryForm();
}

//提交查询表单
function submitQueryForm() {
	$.ajax({
		type : "get",
		dataType : "jsonp",
		jsonp:"callback",    
		jsonpCallback:"success_jsonp",  
		data : $("#queryForm").serialize(),
		url : ip + "/heiMaStudentMsg/queryByParams",
		success : function(result){
			if(result.success){
				//填充表格
				$("#studentMsgInfo-tbody").html("");
				var prefix = '<td align="center">';
				var lastfix = '</td>';
				var currentPage = result.data.currentPage;
				var pageSize = $("#pageSize").val();
				$.each(result.data.data, function (index, bean) {
					if(index%2 == 0){
						var tr = '<tr class="danger">'
					}else{
						var tr = '<tr class="info">'
					}
					tr = tr + prefix + (1 + index + (currentPage-1) * pageSize) + lastfix
						+ prefix + bean["name"] + lastfix
						+ prefix + bean["age"] + lastfix
						+ prefix + (bean["phone"] == undefined ?'暂无':bean["phone"]) + lastfix
						+ prefix + (bean["education"] == undefined ?'暂无':bean["education"]) + lastfix
						+ prefix + (bean["school"] == undefined ?'暂无':bean["school"]) + lastfix
						+ prefix + (bean["graduateTimeStr"] == undefined ?'暂无':bean["graduateTimeStr"]) + lastfix
						+ prefix + (bean["salary"] == undefined ?'努力中...':"¥"+bean["salary"]+".00") + lastfix
						+ prefix + (bean["workMsg"] == undefined || bean["workMsg"] == '' ?'暂无':bean["workMsg"]) + lastfix
						+ prefix + (bean["ruzhiTimeStr"] == undefined ?'暂无':bean["ruzhiTimeStr"]) + lastfix
						+ prefix + (bean["remark"] == undefined || bean["remark"] == ''?'暂无':bean["remark"]) + lastfix 
						+ prefix + 
							'<button type="button" class="btn btn-success btn-sm" onclick="openEditModal('+
									bean["id"]
							+');">编辑</button>'+
							'&nbsp;&nbsp;'+
							'<button type="button" class="btn btn-danger btn-sm" onclick="deleteMsg('+
									bean["id"]
							+');">删除</button>'
						+ lastfix + '</tr>'	
					$("#studentMsgInfo-tbody").append(tr);
				});
				//填充当前页码
				$("#currentPage").val(currentPage);
				$("#currentPageView").val(currentPage);
				$("#allCount").html(result.data.allCount);
				$("#pageCount").html(result.data.pageCount);
			}else{
				$.alert('查询失败！')
			}
		}
	});
}

//提交保存表单（新增/编辑）
function submitSaveForm() {
	if(!validator.form()){
		return;
	}
	$.ajax({
		type : "post",
		dataType : "jsonp",
		jsonp:"callback",    
		jsonpCallback:"success_jsonp",  
		data : $("#saveForm").serialize(),
		url : ip + "/heiMaStudentMsg/save",
		success : function(result){
			if(result.success){
				$.alert({
					title : '提示信息!',
					content : '保存成功！',
					confirm : function() {
						//重置表单
						$("#id").val("");
						resetSaveForm();
						$("#saveModal").modal('hide');
						search();
					}
				});
			}else{
				$.alert('查询失败！');
			}
		}
	});
}

$(function(){
	//初始化校验规则
	validatorInit();
	//初始化日期控件
	datePluginInit(); 
	//为控件增加绑定事件
	bindInit();
	//提交查询表单
	$("#javaBatch").val(javabatch);
	submitQueryForm();
	
})